import os.path

import psutil
import wx
from wx import adv
import threading
import time

from config import ABOUT_TEXT
from functions.note_book import NoteBook
from utils.pyinstaller_get_temp import GetIcoTemp


# 系统托盘
class TaskBarIcon(wx.adv.TaskBarIcon):
    ID_About = wx.NewIdRef()
    ID_Closeshow = wx.NewIdRef()
    id_note_book = wx.NewIdRef()

    def __init__(self, frame):
        wx.adv.TaskBarIcon.__init__(self)
        self.frame = frame

        # 启动监控线程
        self.thread = threading.Thread(target=self.monitor_resources)
        self.thread.daemon = True  # 设置为守护线程，当主程序退出时，线程也会退出
        self.thread.start()

        # 绑定双击托盘图标事件
        self.Bind(wx.adv.EVT_TASKBAR_LEFT_DCLICK, self.on_task_bar_left_click)  # 双击
        self.Bind(wx.EVT_MENU, self.on_about, id=self.ID_About)  # 关于
        self.Bind(wx.EVT_MENU, self.on_close, id=self.ID_Closeshow)  # 托盘右键退出
        self.Bind(wx.EVT_MENU, self.note_book, id=self.id_note_book)  # notebook

        # 创建托盘图标右键菜单
        main_ico = GetIcoTemp().get_note_ico_temp()  # 打包使用
        if not os.path.exists(main_ico):
            main_ico = "files/note.ico"  # 直接运行
        self.SetIcon(wx.Icon(name=main_ico, type=wx.BITMAP_TYPE_ICO),
                     '记事本')

    def monitor_resources(self):
        speed_up = [0, 'KB/s']  # 上传 下载 单位
        speed_down = [0, 'KB/s']  # 上传 下载 单位
        while True:
            # 获取系统资源使用情况
            mem = psutil.virtual_memory()
            mem_usage = mem.percent  # 内存使用率
            cpu_percent = psutil.cpu_percent(interval=1)  # CPU使用率
            disk_usage = psutil.disk_usage("C:\\")
            disk_usage_percent = disk_usage.percent  # C盘使用率

            allif = psutil.net_io_counters(pernic=True)
            WIFI = allif.get('WLAN')
            l_send = WIFI.bytes_sent
            l_recv = WIFI.bytes_recv
            time.sleep(1)  # 1秒,模拟定时器
            allif = psutil.net_io_counters(pernic=True)
            WIFI = allif.get('WLAN')
            speed_up[0] = WIFI.bytes_sent - l_send
            speed_down[0] = WIFI.bytes_recv - l_recv

            up = speed_up[0] / 1024  # KB
            down = speed_down[0] / 1024  # KB
            if up >= 1024:
                up = up / 1024
                speed_up[1] = 'MB/s'
            else:
                speed_up[1] = 'KB/s'

            if down >= 1024:
                down = down / 1024
                speed_down[1] = 'MB/s'
            else:
                speed_down[1] = 'KB/s'

            # 更新托盘提示信息
            resources = (f"内存使用率：{mem_usage}%\n"
                         f"CPU使用率：{cpu_percent}%\n"
                         f"C盘使用率：{disk_usage_percent}%\n"
                         f"上传  网速：{'%.1f %s' % (up, speed_up[1])}\n"
                         f"下载  网速：{'%.1f %s' % (down, speed_down[1])}"
                         )
            self.update_resources(resources)

    def update_resources(self, resources):
        # 这个函数用于更新托盘图标的提示信息
        # 你需要确保这个函数是在主线程中调用的，因为wxPython的组件只能在主线程中操作
        main_ico = GetIcoTemp().get_note_ico_temp()  # 打包使用
        if not os.path.exists(main_ico):
            main_ico = "files/note.ico"  # 直接运行
        wx.CallAfter(self.SetIcon, wx.Icon(name=main_ico, type=wx.BITMAP_TYPE_ICO), resources)

    # 双击
    def on_task_bar_left_click(self, event):
        NoteBook(None, '记事本').Show()

    def on_close(self, event):
        self.Destroy()
        self.frame.Close(True)

    # 托盘图标右键菜单
    def CreatePopupMenu(self):
        menu = wx.Menu()
        menu.Append(self.id_note_book, '记事本')

        menu.Append(self.ID_Closeshow, '退出')
        menu.Append(self.ID_About, '关于')

        return menu

    def on_about(self, event):
        # 创建并显示关于对话框
        dlg = wx.MessageDialog(self.frame, ABOUT_TEXT, " ", wx.OK | wx.ICON_INFORMATION)
        dlg.ShowModal()  # 显示对话框
        dlg.Destroy()  # 销毁对话框

    def note_book(self, event=None):
        NoteBook(None, '记事本').Show()
